// pages/user/withdrawals.js
var app = getApp();
var server = require("../../../utils/server.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance:11.11,
    form:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var balance = app.globalData.userInfo.user_money;
    that.setData({
      balance:balance,
    })

  },


//form表单提交
  formSubmit:function(e){
    console.log(e)
    var that = this;
    var card,user,bank,user_money;
    card = that.data.card;
    user = that.data.user;
    bank = that.data.bank;
    user_money = that.data.user_money;
    if(!card){
      wx.showToast({
        title: '请输入合法银行卡号',
        icon:'none',
      })
      return;
    }
    if (!user) {
      wx.showToast({
        title: '请输入合法持卡人姓名',
        icon: 'none',
      })
      return;
    }
    if (!bank) {
      wx.showToast({
        title: '请输入合法银行名称',
        icon: 'none',
      })
      return;
    }
    if (!user_money) {
      wx.showToast({
        title: '请输入合法金额',
        icon: 'none',
      })
      return;
    }
    var form = that.data.form;
    if (!form) {
      wx.showToast({
        title: '提交过于频繁',
        icon: "none",
      })
      return;
    }
    that.setData({
      form: false,
    })
    var userid = app.globalData.userInfo.user_id;
    server.postJSON('/user/withdrawals', { user_id: userid, money: e.detail.value.money, bank_name: e.detail.value.bank, account_bank: e.detail.value.card, account_name:e.detail.value.user},function(e){
      if(e.data.code == 1){
        wx.showToast({
          title: e.data.msg,
          icon:'none',
        })
        that.setData({
          form:true,
        })
        setTimeout(function(){
          wx.navigateBack({
            delta:1,
          })
        },1000)
      }
      if(e.data.code < 0){
        wx.showToast({
          title: e.data.msg,
          icon:"none",
        })
        that.setData({
          form: true,
        })
        return;
      }
    })

  },

  //blur校验
  card:function(e){
    var that = this;
    var reg = /^([1-9]{1})(\d{14}|\d{18})$/;
    if(!reg.test(e.detail.value)){
      wx.showToast({
        title: '银行卡号有误',
        icon:'none',
      })
      that.setData({
        card:false,
      })
      return;
    }else{
      that.setData({
        card: true,
      })
      return;
    }
  },
  user: function (e) {
    var that = this;
    if (e.detail.value == "") {
      wx.showToast({
        title: '持卡人姓名为空',
        icon: 'none',
      })
      that.setData({
        user: false,
      })
      return;
    }else{
      that.setData({
        user: true,
      })
      return;
    }
  },
  bank: function (e) {
    var that = this;
    if (e.detail.value == "") {
      wx.showToast({
        title: '银行名称为空',
        icon: 'none',
      })
      that.setData({
        bank: false,
      })
      return;
    } else {
      that.setData({
        bank: true,
      })
      return;
    }  },
  money: function (e) {
    var that = this;
    var reg = /^\+?[1-9][0-9]*$/;
    if (!reg.test(e.detail.value)) {
      wx.showToast({
        title: '请输入合法数字',
        icon: 'none',
      })
      that.setData({
        user_money: false,
      })
      return;
    } else {
      that.setData({
        user_money: true,
      })
      return;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})